import os
import time
import shutil
import sys

PATH_DATABASE = os.path.join('db', 'Person.sqlite')
PATH_BACKUP = os.path.join('..', 'db_backup')

if not os.path.isdir(PATH_BACKUP):
    os.mkdir(PATH_BACKUP)
    print("made dir !!")

last_edit_time = os.path.getmtime(PATH_DATABASE)
while True:
    edit_time = os.path.getmtime(PATH_DATABASE)
    if last_edit_time < edit_time:
        last_edit_time = edit_time
        try:
            backup_path = os.path.join(PATH_BACKUP, 'Person-%s.sqlite' % time.strftime('%m%d-%H%M%S'))
            shutil.copy(PATH_DATABASE, backup_path)
            print("copied to %s !!" % backup_path)

        except:
            print('error: ', sys.exc_info())
            
    time.sleep(60)
